/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;


public class sec extends HttpServlet {

    Connection c;
   
    String name,rollno,course,rnn;
    HttpServletResponse response;
    
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet sec</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet sec at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String rn=request.getParameter("v");
        if(getc())
        {
            out.println("connection sucess");
            
            if(retv(rn,out))
            {
                
                see(out);
            }
            else
            {
            out.println("login failed");
            }
            
        }
        else
        {
            out.println("connention failed");
        }
        
        
        processRequest(request, response);
    }

    public boolean retv(String rn,PrintWriter out)
    {
        
        try{
        Statement st = c.createStatement();
        ResultSet res= st.executeQuery("SELECT * FROM  stu where Rollno="+rn);
        if(res.next())
        {
            //out.println("i am in retrv");
            
               name = res.getString(1);
               //out.println(name);
                course = res.getString(3);
                
                rnn=res.getString(2);
               // out.println(course);
               return true;
            }
        
                
        
        
        }
        catch(Exception e)
           
        {
           
         out.println("im in catch block");
        }
        return false;
        
    }
    
    public void see(PrintWriter out)
    {
        try
        {
        //PrintWriter out = response.getWriter();
       // out.println("im here");
        out.print("<html><body>NAME:"+name+"<br />");
            out.print("ROLLNO :"+rnn+"<br />");
            out.print("Course :"+course+"</body></html>");
           
        }
        catch(Exception e)
        {
            
        }
        
        
    }
    
    
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
      String s1=request.getParameter("nm");
      String s2=request.getParameter("rno");
      String s3=request.getParameter("cr");
      
      if(getc())
      {
         out.println("connection sucees");
        if( reg(s1,s2,s3))
            out.println("data successful inserted ");
         
      }
      else
      {
          out.println("connnection failed");
      }
      
      
      
      
      
    }

    
    public boolean getc()
    {
        String url="jdbc:mysql://localhost:3306/";
   String driver;
    String db="student";
    try
        {
        driver="com.mysql.jdbc.Driver";
        Class.forName(driver);
        c=DriverManager.getConnection(url+db,"root","");
        return true;
        }
    catch(Exception e)
        {
            System.out.println("Exception is :"+e);
        }

        return false;
        
    }
    public boolean reg(String s1,String s2,String s3)
    {
          try
        {
            PreparedStatement pst=c.prepareStatement("insert into stu(NAME,ROLLNO,COURSE)"+"values(?,?,?)");
            pst.setString(1,s1);
            pst.setString(2,s2);
            pst.setString(3,s3);
            pst.executeUpdate();
            return true;
        }
        catch(Exception se)
        {
            System.out.println(se);
        }

    return false;
    }
    
    
    
   
    
    
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
